<?php
/**
 * 《合同信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/EmpUtil.class.php');

class Salary extends EmpUtil {
	public $tbl = Constant::tbl_emp_salary;
	
	public $type = array(
		'-' => '请选择',
		'1' => '固定工资',
		'2' => '浮动工资',
		'3' => '小时工资',

		'9' => '其它',
	);
	
	public $job = array(
		'-' => '请选择',
		'1' => '5.5天制',
		'2' => '6天制',
		'3' => '编程、设计',
		'4' => '技工、普工',
		
		'9' => '其它',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '申请',
		'2' => '等待',
		'3' => '执行',
		'4' => '完成',

		'9' => '其它',
	);
	
	public $orderby = array(
		'-' => '请选择',
		'emp' => '员工',
		'hour' => '时薪',

		'day' => '日薪',
		'amount' => '月薪',
		's1' => '部门奖金',
		's2' => '职务工资',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('type_option', $this->type);
		
		$controller->assign_by_ref('job_option', $this->job);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'emp/salary/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {		
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];
		
		$type_s = $request['type_s'];

		$my = $this->_my_add();
		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}
		
		if (!empty($date1_s)) {
			$where .= " AND t1 >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND t2 < '" . $date2_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'emp/salary/list');
		return true;
	}
	
	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		$controller->display($request, 'emp/salary/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$this->_my_emp($controller, $id);

		$controller->display($request, 'emp/salary/add');
		return true;
	}
	
	function _my_emp($controller, $id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $id);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {			
			$row = $this->_view(null, $row);
			
			$ret[] = $row;
		}

		$controller->assign_by_ref('my', $ret);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$hour = $request['hour'];
		$day = $request['day'];
		$amount = $request['amount'];

		$s1 = $request['s1'];
		$s2 = $request['s2'];
		$k1 = $request['k1'];
		$overtime = $request['overtime'];
		
		$c_s1 = $request['c_s1'];
		$c_day = $request['c_day'];
		
		$t1 = $request['t1'];
		$t2 = $request['t2'];
		
		$type = $request['type'];
		$job = $request['job'];
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, type, job, hour, day, c_day, s1, c_s1, s2, k1, overtime, amount, t1, t2, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $type, $job, $hour, $day, $c_day, $s1, $c_s1, $s2, $k1, $overtime, $amount, $t1, $t2, $content, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		$id = $db->insert_id();

		$request['id_s'] = $id;
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
		
		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/salary/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$hour = $request['hour'];
		$day = $request['day'];
		$amount = $request['amount'];

		$s1 = $request['s1'];
		$s2 = $request['s2'];
		$k1 = $request['k1'];
		$overtime = $request['overtime'];
		
		$c_s1 = $request['c_s1'];
		$c_day = $request['c_day'];
		
		$t1 = $request['t1'];
		$t2 = $request['t2'];
		
		$type = $request['type'];
		$job = $request['job'];
		$content = addslashes($request['content']);
		
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', type = '%s', job = '%s', hour = '%s', day = '%s', c_day = '%s', s1 = '%s', c_s1 = '%s', s2 = '%s', ";
		$fmt .= "k1 = '%s', overtime = '%s', amount = '%s', t1 = '%s', t2 = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $type, $job, $hour, $day, $c_day, $s1, $c_s1, $s2, $k1, $overtime, $amount, $t1, $t2, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'emp/salary/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/salary/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['type_t'] = $this->type[$row['type']];
		$row['job_t'] = $this->job[$row['job']];
		
		// $row['d1'] = $this->date_1($row['t1'], $row['t2']);
		// $row['d2'] = $this->date_1(date("Y-m-d"), $row['t2']);
		
		// $row['d3'] = $this->_t3($row['t2']);
		$row = $this->_view_t($row);
		
		return $row;
	}
	
	function view_emp($controller, $request) {
		$this->pretreat($controller);
	
		$emp = $request['emp'];
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $emp);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$controller->assign_by_ref('result', $row);
		}
	
		$controller->display($request, 'emp/salary/view');
		return true;
	}

	function delete($controller, $request) {
		$this->pretreat($controller);

		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->_view($id);
			if (!$this->_is_delete($row)) {
				continue;
			}

			$this->_delete3($id);
		}
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['emp'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function _delete3($id) {
		$db = newdb($this->dsn_w);
		if ($this->_is_admin()) {
			$fmt = "DELETE FROM %s WHERE id = '%s'";
		} else {
			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
		}
		
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}
	
	function num($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(*) t FROM %s WHERE emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		// debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		return $row['t'];
	}
	
	function _leave($emp, $t2) {
		$db = newdb($this->dsn_w);		
		$fmt = "UPDATE %s SET status = '2', t2 = '%s', modifytime = now() WHERE emp = '%s' AND status = 1";
		$sql = sprintf($fmt, $this->tbl, $t2, $emp);
		debug($sql);
		$db->query($sql);
	}
	
	function z($controller, $request) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE status = '1'";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$emp = Common::id(Constant::tbl_emp, $row['emp']);
			if ($emp['status'] == '1') {
				echo ".";
				continue;
			}

			$fmt = "UPDATE %s SET status = '2', modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $row['id']);
			// debug($sql);
			$db->query($sql);
			echo "U";
		}

		logdate($controller, $request);
		return true;
	}
	
	
	function t($controller, $request) {
		
		$file = "/Users/sunxiaoye/Downloads/11.csv";
		
		$fd = fopen($file, "r");
		while (!feof($fd)) {
			$buf = $this->_to_utf8(fgets($fd));
			// echo $buf;
			$this->_line_2($buf);
		}
		fclose($fd);
		
		logdate($controller, $request);
		return true;
	}
	
	function _line_2($buf) {
		if (strlen($buf) < 5 || preg_match("/^#/i", $buf)) {
			return;
		}
	
		$a = preg_split("/(\t|,|;)/", $buf);
		$no = trim($a[0]);
	
		if (!preg_match("/^(\d+)$/i", $no)) {
			return;
		}
	
		// print_r($a);
		// echo $a[1] . "\n";
		
		$emp = _trim($a[1]);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$hour = _trim($a[6]);
		$day = _trim($a[7]);
		$amount = _trim($a[12]);
		$s1 = _trim($a[8]);
		$s2 = _trim($a[10]);
		$k1 = _trim($a[11]);
		
		$content = _trim($a[13]);
	
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, hour, day, s1, s2, k1, amount, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $hour, $day, $s1, $s2, $k1, $amount, $content, '101');
		debug($sql);
		$db->query($sql);
	}
	
	function view_emp2($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$ret = $this->_list_emp($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'emp/salary/_list');
		return true;
	}
}

function _trim($s) {
	$s = trim($s);
	if (empty($s)) {
		return '';
	}

	$s = preg_replace("/[\s|\"|\'|,|-]+/i", "", $s);

	return $s;
}
?>